import { wxToPromise } from "./wx";
import APIConfig from "../config/api"
import Http from "../utils/http"

class FileUploader extends Http {
    static async upload(filePath, key = 'file') {
        let res
        try {
            res = await wxToPromise('uploadFile', {
                url: APIConfig.baseUrl + '/v1/file',
                filePath: filePath,
                name: key,
                method: 'POST',
            })
        } catch (e) {
            FileUploader._showError(-1);
            throw new Error(e.errMsg)
        }


        const serviceData = JSON.parse(res.data)

        if(res.statusCode !== 201){
            FileUploader._showError(serviceData.error_code, serviceData.message)
            throw new Error(serviceData.message)
        }

        return serviceData.data

    }
}


export default FileUploader